<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Admin/classes/PhpFox_ComponentSiteContent.class.php, $Author:  $ */



/** Component for showing site content items and editing them
 *   
 * @version $Id: PhpFox_ComponentSiteContent.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.admin
 */
class PhpFox_Mod_Admin_ComponentSiteContent extends BaseComponent
{
    function process()
    {
    	if ( isset($_POST['localLang']) )
    	{
    		$sLocalLang = strip_tags($_POST['localLang']);
    	}
    	else
    	{
    		$sLocalLang = App::getLang();
    	}	
    
        $oAdminService = $this->_oModule->getService('Admin');
        /* @var $oAdminService PhpFox_Mod_Admin_ServiceAdmin */
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $aOut = array();
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        if (!$oSecurityService->hasStaffPerm('MENU_edit_site_items'))
        {
            App::gotoUrl('admin.main');
        }

        if ($sFile = $oReq->get('file'))
        {
            $sDir = $oReq->get('dir');
            $sPath = 'file/';
            if ($sDir)
            {
                $sPath .= $sDir.'/';
            }
            if ($sDir != 'txt')
            {
                $sPath .= $oReq->get('lang').'/';
            }
            $sPath .= $sFile;
            $sPath .= '.html';
            
            /*
            if ($sDir == 'txt')
            {
                $sPath .= '.txt';
            }
            else
            {
                $sPath .= '.html';
            }
            */
            
            if (!$oAdminService->file_isset($sDir, $sFile))
            {
                App::gotoUrl('admin.main.content');
            }
            $bWritable = CFile::isWritable(App::getParam('path').$sPath);
            if (($oReq->getAction() == 'save') && ($bWritable))
            {
                $bWrited = $oAdminService->writeToFile($oReq->get('content', ''), $sPath);
                $aParams = array
                (
                    'dir'  => $sDir,
                    'file' => $sFile,
                    'lang' => $oReq->get('lang'),
                );
                if ($bWrited)
                {
                    App::gotoUrl('admin.popup.contenteditor', $aParams, App::format('Mod_Admin.mess.file_writed'), false, 'editor');
                }
                App::gotoUrl('admin.popup.contenteditor', $aParams, App::format('Mod_Admin.error.file_not_writed'), true, 'editor');
            }
            $oTpl->assign(array(
                'sFile'     => $sFile,
                'sDir'      => $sDir,
                'bWriteble' => $bWritable,
                'sFilePath' => $sPath,
                'langFile' => $oReq->get('lang'),
                'sContent'  => $oAdminService->readFromFile(App::getParam('path').$sPath),
            ));
            return $oTpl->fetch('SiteContentEditor.html');
        }
        $aMainItems = array();
        foreach (App::getParam('Mod_Admin.content_pages', 'main') as $sTitle => $aFile)
        {
            $sPath = 'file/';
            if ($aFile['dir'])
            {
                $sPath .= $aFile['dir'].'/';
            }
            if ($aFile['dir'] != 'txt')
            {
                $sPath .= $sLocalLang.'/';
            }
            $sPath .= $aFile['file'];
            if ($aFile['dir'] == 'txt')
            {
                $sPath .= '.txt';
            }
            else
            {
                $sPath .= '.html';
            }
            $aMainItems[] = array
            (
                'title'    => App::format($sTitle),
                'file'     => $aFile['file'],
                'dir'      => $aFile['dir'],
                'lang' => $sLocalLang,
                'writeble' => CFile::isWritable(App::getParam('path').$sPath),
            );
        }
        $aEmailItems = array();
        foreach (App::getParam('Mod_Admin.content_pages', 'email') as $sTitle => $aFile)
        {
            $sPath = 'file/';
            if ($aFile['dir'])
            {
                $sPath .= $aFile['dir'].'/';
            }
            if ($aFile['dir'] != 'txt')
            {
                $sPath .= $sLocalLang.'/';
            }
            $sPath .= $aFile['file'];
            if ($aFile['dir'] == 'txt')
            {
                $sPath .= '.txt';
            }
            else
            {
                $sPath .= '.html';
            }
            $aEmailItems[] = array
            (
                'title'    => App::format($sTitle),
                'file'     => $aFile['file'],
                'dir'      => $aFile['dir'],
                'lang' => $sLocalLang,
                'writeble' => CFile::isWritable(App::getParam('path').$sPath),
            );
        }
        $aDropDownItems = array();
        foreach (App::getParam('Mod_Admin.content_pages', 'dropdown') as $sTitle => $aFile)
        {
            $sPath = 'file/';
            if ($aFile['dir'])
            {
                $sPath .= $aFile['dir'].'/';
            }
            if ($aFile['dir'] != 'txt')
            {
                $sPath .= $sLocalLang.'/';
            }
            $sPath .= $aFile['file'];
            $sPath .= '.html';
            
            /*
            if ($aFile['dir'] == 'txt')
            {
                $sPath .= '.txt';
            }
            else
            {
                $sPath .= '.html';
            }
            */
            
            $aDropDownItems[] = array
            (
                'title'    => App::format($sTitle),
                'file'     => $aFile['file'],
                'dir'      => $aFile['dir'],
                'lang' => $sLocalLang,
                'writeble' => CFile::isWritable(App::getParam('path').$sPath),
            );
        }
        $oTpl->assign(array(
            'aItems' => $aMainItems,
        ));
        $aOut['main'] = $oTpl->fetch('SiteContentItems.html');
        $oTpl->assign(array(
            'aItems' => $aEmailItems,
        ));
        $aOut['email'] = $oTpl->fetch('SiteContentItems.html');
        $oTpl->assign(array(
            'aItems' => $aDropDownItems,
        ));
        $aOut['dropdown'] = $oTpl->fetch('SiteContentItems.html');

        return $aOut;
    }
}
?>